<?php
/*=============================================================================
|| ##################################################################
||	phpFoX Konsort
|| ##################################################################
||
||	Copyright		: (C) 2005-2007 PHPFOX Limited a Reality Fox Creation
||	Contact			: info@phpfox.com
||
||	- phpFoX and all of its source code and files are protected by Copyright Laws.
||
||	- The license for phpFoX permits you to install this software on a single domain (.com, .org, .net, etc.).
||
||	- You may also not remove this copyright screen which shows the copyright information and credits for phpFoX (RealityFOX Creations).
||
||	- phpFoX Konsort is NOT a FREE software - http://www.phpfox.com/license/
||
|| ##################################################################
=============================================================================*/

/* $File: ./include/modules/Music/classes/PhpFox_ComponentBrowseMusicians.class.php, $Author:  $ */



/** Component for browsing musicians
 *
 * @version $Id: PhpFox_ComponentBrowseMusicians.class.php 297 2008-02-07 15:05:58Z phpfoxsv $
 * @package module.music
 */
class PhpFox_Mod_Music_ComponentBrowseMusicians extends BaseComponent
{
    function process()
    {
        $oItmAlbum = App::getModuleItem('Music', 'Album');
        $oUrl      = App::getUrl();

        $oMusicianItem = &$this->_oModule->getItem('Musician');
        /* @var $oMusicianItem PhpFox_Mod_Music_ItemMusician */
        $oMusicService = &$this->_oModule->getService('Music');
        /* @var $oMusicService PhpFox_Mod_Music_ServiceMusic */
        $oSecurityService = &App::getModuleService('Account', 'Security');
        /* @var $oSecurityService PhpFox_Mod_Account_ServiceSecurity */
        $oReq = &App::getRequest();

        $oTpl = &$this->_oModule->getTemplate();
        $aOut = array();

        $oSecurityService->checkPerm('music', array('read_only', 'full'));
        $oSecurityService->checkAccess('music');
        $bStaff = $oSecurityService->isStaff();
        $sFirstLetter = $oReq->get('l');
        $sChoice = strtoupper($sFirstLetter);
        $sFirstLetter = Database::escape($sFirstLetter);
        $aCond = array();
        if ($oReq->getAction() == 'delete' AND $bStaff)
        {
            $iMusicianId = $oReq->get('mid');
            list($aAlbums, $iTotalAlbums) = $oItmAlbum->getList(array('musician_id' => '= ' . $iMusicianId));
            foreach ($aAlbums as $aAlbum)
            {
                $oMusicService->deleteAlbum($aAlbum['album_id']);
            }
            $oMusicianItem->delete($iMusicianId);
            $oUrl->clearParams('act');
            $oUrl->clearParams('mid');
            App::forward($oUrl->getUrl());
        }
        if ($iMusicianId = $oReq->getInt('feature'))
        {
            if ($bStaff)
            {
                $oMusicService->setMusicianFeatureState($iMusicianId, true);
            }
            $oUrl = &App::getUrl();
            $oUrl->clearParams('feature');
            App::forward($oUrl->getUrl());
        }
        if ($iMusicianId = $oReq->getInt('unfeature'))
        {
            if ($bStaff)
            {
                $oMusicService->setMusicianFeatureState($iMusicianId, false);
            }
            $oUrl = &App::getUrl();
            $oUrl->clearParams('unfeature');
            App::forward($oUrl->getUrl());
        }
        if ($sFirstLetter == 'numbers')
        {
            $sChoice = '0-9';
            $sCond = '';
            for ($i = 0; $i <= 9; $i++)
            {
                $sCond .= '(LOWER(SUBSTRING(mm.musician_name, 1, 1)) = "'.$i.'") OR ';
            }
            $sCond = rtrim($sCond, ' OR ');
            $aCond[] = '('.$sCond.')';
        }
        elseif ($sFirstLetter)
        {
            $aCond[] = 'LOWER(SUBSTRING(mm.musician_name, 1, 1)) = "'.$sFirstLetter.'"';
        }
        else
        {
            $sChoice = App::format('Mod_Music.all_musicians');
        }
        $sSort = 'musician_name';

        $aMusicians = $oMusicService->getMusiciansList($aCond, $sSort);
        //list($aMusicians) = $oMusicianItem->getList($aCond, 0, 0, $sSort);

        $oTpl->assign(array(
            'aLetters'   => App::getParam('Mod_Music.musicians_browse_letters'),
            'aMusicians' => $aMusicians,
            'bIsStaff'   => $bStaff,
            'sChoice'    => $sChoice,
        ));
        $aOut['alphabetical_index'] = $oTpl->fetch('AlphabeticalIndex.html');
        $aOut['list'] = $oTpl->fetch('BrowseMusician.html');
        return $aOut;
    }
}
?>